<template>
	<view>
		<view class="good-list">
			<view v-for="(item,index) in goodsList" :key="index" @click="gotoDetail(item)">
				<my-goods :item="item"></my-goods>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				//请求参数对象
				queryObj:{
					query:'',
					cid:'',
					pageNum:1,
					pageSize:10
				},
				goodsList:[],
				total:0,
				isload:false
			}
		},
		onLoad(option) {
			this.queryObj.query = option.query || ''; 
			this.queryObj.cid = option.cid || '';
			this.getGoodsList();
		},
		methods: {
			//前往详情页面
			gotoDetail(data){
				uni.navigateTo({
					url:'/subpkg/good_detail/good_detail?goods_id='+data.goods_id
				})
			},
			//下拉刷新事件
			onPullDownRefresh(){
				//重置所有数据
				this.queryObj.pageNum = 1;
				this.total = 0;
				this.isload = false;
				this.goodsList = []
				//重新发起数据请求
				this.getGoodsList(()=> uni.stopPullDownRefresh())
			},
			
			//上拉触底事件
			onReachBottom(){
				if(this.queryObj.pageNum * this.queryObj.pageSize >= this.total){
					return uni.$showMsg('已经到底了咯~');
				}
				if(this.isload){
					return;
				}
				//让页码自增加一
				this.queryObj.pageNum++;
				this.getGoodsList();
			},
			async getGoodsList(cb){
				this.isload = true;
				const {data:res} = await uni.$http.get('/api/public/v1/goods/search',this.queryObj);
				this.isload = false;
				cb && cb();
				if(res.meta.status !== 200){
					return uni.$showMsg();
				}else{
					this.total = res.message.total;
					this.goodsList = [...this.goodsList,...res.message.goods];
				}
			}
		}
	}
</script>

<style lang="scss">
	
</style>
